package cn.lkx.eduservice.controller;


import cn.lkx.commonutils.R;
import cn.lkx.eduservice.entity.EduComment;
import cn.lkx.eduservice.service.EduCommentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 评论 前端控制器
 * </p>
 *
 * @author lkx
 * @since 2022-05-12
 */
@RestController
@RequestMapping("/eduservice/comment")
@CrossOrigin
public class EduCommentController {
    @Autowired
    private EduCommentService commentService;

    //根据课程id 分页查询评论列表
    @GetMapping("pageFind/{page}/{limit}/{courseId}")
    public R pageFind(@PathVariable Long page,@PathVariable Long limit,@PathVariable String courseId){
        QueryWrapper<EduComment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id",courseId).orderByDesc("gmt_create");
        Page<EduComment> pageParam = new Page<>(page,limit);
        commentService.page(pageParam,queryWrapper);
        //返回个map
        Map<String,Object> map = new HashMap<>();
        map.put("items",pageParam.getRecords());
        map.put("pages",pageParam.getPages());
        map.put("total",pageParam.getTotal());
        map.put("current",pageParam.getCurrent());
        map.put("hasPrevious",pageParam.hasPrevious());
        map.put("hasNext",pageParam.hasNext());
        return R.ok().data(map);
    }
    //添加评论方法
    @PostMapping("addComment")
    public R addComment(@RequestBody EduComment eduComment){
        boolean save = commentService.save(eduComment);
        if(save){
            System.out.println("执行===========================================添加评论了");
            return R.ok();
        }else {
            return R.error();
        }
    }
}

